package com.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.constant.Constant;
import com.demo.entity.MedicineCate;
import com.demo.entity.Vo.MedicneCateVo;
import com.demo.mapper.MedicineCateMapper;
import com.demo.response.Result;
import com.demo.service.IMedicineCateService;
import com.demo.service.IMedicineService;
import com.demo.service.IUserService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 预防药物类目（型） 服务实现类
 *
 * @author ZM
 * @since 2020-11-20
 */
@Service
@Slf4j
public class MedicineCateServiceImpl extends ServiceImpl<MedicineCateMapper, MedicineCate> implements IMedicineCateService {
    @Resource
    MedicineCateMapper cateMapper;
    @Autowired
    IUserService userService;
    @Autowired
    IMedicineService medicineService;

    /**
     * 新增预防药物类目（型）
     *
     * @param medicineCate 实体对象
     * @return
     */
    @Override
    public Result addMedicineCate(MedicineCate medicineCate) {
        log.info("对象" + medicineCate);
        boolean flag = this.save(medicineCate);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * 编辑预防药物类目（型）
     *
     * @param medicineCate 实体对象
     * @return
     */
    @Override
    public Result updateMedicineCate(MedicineCate medicineCate) {
        if (medicineCate.getMedicineCateId() == null || medicineCate.getUpdatedUserId() == null) {
            return Result.error(Constant.KEY_ID);
        }
        medicineCate.setUpdatedTime(new Date());
        boolean flag = this.updateById(medicineCate);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * 删除预防药物类目（型）
     *
     * @param medicineCateId 类目ID
     * @param deletedUserId  删除人ID
     * @return
     */
    @Override
    public Result deleteMedicineCate(Integer medicineCateId, Integer deletedUserId) {
        //获取该类目是否存在药物，存在就不能删除
        boolean status = medicineService.getAll(medicineCateId);
        if (status == true) {
            MedicineCate medicineCate = new MedicineCate();
            medicineCate.setMedicineCateId(medicineCateId);
            medicineCate.setDeletedUserId(deletedUserId);
            medicineCate.setDeletedTime(new Date());
            boolean flag = this.updateById(medicineCate);
            if (flag == false) {
                return Result.error(Constant.DB_ERROR);
            }
            return Result.success();
        }
        return Result.error("该类目下存在药物，不能删除");

    }

    /**
     * 预防药物类目（型）列表
     *
     * @param pageNumber     页码
     * @param pageSize       每页数量
     * @param medicineCateId 类目ID
     * @param category       类目名称
     * @return
     */
    @Override
    public Result medicineCateList(Integer pageNumber, Integer pageSize, Integer medicineCateId, String category) {
        IPage<MedicineCate> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<MedicineCate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(medicineCateId != null, "medicine_cate_id", medicineCateId);
        queryWrapper.like(category != null && !"".equals(category), "category", category);
        queryWrapper.isNull("deleted_user_id");
        queryWrapper.orderByAsc("item_sort");
        page = cateMapper.selectPage(page, queryWrapper);
        List<MedicineCate> list = new ArrayList<>(page.getRecords());

        List<MedicneCateVo> roleVoList = new ArrayList<>();
        for (MedicineCate cate : list) {
            MedicneCateVo cateVo = new MedicneCateVo();
            BeanUtils.copyProperties(cate, cateVo);
            cateVo.setCreateName(userService.getUnameById(cate.getCreatedUserId()));
            cateVo.setUpdateName(userService.getUnameById(cate.getUpdatedUserId()));
            roleVoList.add(cateVo);
        }
        PageInfo<MedicneCateVo> pageInfo = new PageInfo<>(roleVoList);
        pageInfo.setTotal(page.getTotal());
        pageInfo.setPages((int) page.getPages());
        if (pageNumber.equals(page.getPages())) {
            pageInfo.setIsLastPage(false);
        } else {
            pageInfo.setIsLastPage(true);
        }
        return Result.success(pageInfo);
    }

    /**
     * 根据类目ID，查询名称
     *
     * @param id 类目ID
     * @return
     */
    @Override
    public String getCateNameById(Integer id) {
        return cateMapper.getCateNameById(id);
    }


}
